//
//  SDHSportsDetailHeaderView.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/11.
//

#import "SDHSportsDetailHeaderView.h"
#import "SDHSportsWinRateView.h"
#import "SDHSportsInteDetailModel.h"
#import "SDHSportsInteListModel.h"
#import "UIImageView+YQAdd.h"
#import "SDHSportsRatioResultView.h"

const static CGFloat kTeamInfoHeight = 120;
const static CGFloat kWinRateLabelWidth = 50;
const static CGFloat kWinRateDescWidth = 16;

@interface SDHSportsDetailHeaderView ()

@property (nonatomic, strong) UIImageView *leftAvatarImg;
@property (nonatomic, strong) UILabel *leftNameLb;
@property (nonatomic, strong) UILabel *leftScoreLb;
@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UIImageView *rightAvatarImg;
@property (nonatomic, strong) UILabel *rightNameLb;
@property (nonatomic, strong) UILabel *rightScoreLb;
@property (nonatomic, strong) UILabel *leftRateTitle;
@property (nonatomic, strong) UILabel *rightRateTitle;

@property (nonatomic, strong) SDHSportsWinRateView *sratioView;
@property (nonatomic, strong) SDHSportsWinRateView *recentSratioView;
@property (nonatomic, strong) SDHSportsWinRateView *seasonSratioView;

@property (nonatomic, strong) SDHSportsRatioResultView *leftRatioResultView;
@property (nonatomic, strong) SDHSportsRatioResultView *rightRatioResultView;

@end

@implementation SDHSportsDetailHeaderView

- (id)init {
    if (self = [super init]) {
        UIView *hView = self;
        UIView *teamInfoView = [UIView new];
        teamInfoView.backgroundColor = [UIColor whiteColor];
        teamInfoView.clipsToBounds = YES;
        teamInfoView.layer.cornerRadius = 5;
        teamInfoView.layer.borderColor = [UIColor grayColor].CGColor;
        teamInfoView.layer.borderWidth = 0.5;
        [hView addSubview:teamInfoView];
        
        // left
        UIImageView *leftAvatarImg = [UIImageView new];
        leftAvatarImg.image = [UIImage imageNamed:@"touxiang"];
        leftAvatarImg.userInteractionEnabled = YES;
        [teamInfoView addSubview:leftAvatarImg];
        self.leftAvatarImg = leftAvatarImg;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
        [leftAvatarImg addGestureRecognizer:tap];
        
        UILabel *leftNameLb = [UILabel new];
        leftNameLb.textAlignment = NSTextAlignmentCenter;
//        leftNameLb.backgroundColor = [UIColor greenColor];
        leftNameLb.font = [UIFont systemFontOfSize:15];
        leftNameLb.numberOfLines = 0;
        [teamInfoView addSubview:leftNameLb];
        leftNameLb.text = @"sdlfkjsd";
        self.leftNameLb = leftNameLb;
        
        UILabel *leftScoreLb = [UILabel new];
        leftScoreLb.textAlignment = NSTextAlignmentCenter;
//        leftScoreLb.backgroundColor = [UIColor grayColor];
        leftScoreLb.font = [UIFont boldSystemFontOfSize:18];
        leftScoreLb.textColor = UIColorFromRGB(182, 182, 182);
        leftScoreLb.numberOfLines = 0;
        leftScoreLb.text = @"111";
        [teamInfoView addSubview:leftScoreLb];
        self.leftScoreLb = leftScoreLb;
        
        // center
        UIImageView *vsImgView = [UIImageView new];
        vsImgView.image = [UIImage imageNamed:@"vs"];
        [teamInfoView addSubview:vsImgView];
        
        UILabel *timeLb = [UILabel new];
        timeLb.textAlignment = NSTextAlignmentCenter;
        timeLb.backgroundColor = [UIColor grayColor];
        timeLb.font = [UIFont systemFontOfSize:15];
        timeLb.textColor = [UIColor whiteColor];
        timeLb.numberOfLines = 1;
        timeLb.text = @" 2021-3-11 15:00";
        timeLb.clipsToBounds = YES;
        timeLb.layer.cornerRadius = 5;
        [teamInfoView addSubview:timeLb];
        self.timeLb = timeLb;
        
        // right
        UIImageView *rightAvatarImg = [UIImageView new];
        rightAvatarImg.image = [UIImage imageNamed:@"touxiang"];
        rightAvatarImg.userInteractionEnabled = YES;
        [teamInfoView addSubview:rightAvatarImg];
        self.rightAvatarImg = rightAvatarImg;
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
        [rightAvatarImg addGestureRecognizer:tap];
        
        UILabel *rightNameLb = [UILabel new];
        rightNameLb.textAlignment = NSTextAlignmentCenter;
//        rightNameLb.backgroundColor = [UIColor greenColor];
        rightNameLb.font = [UIFont systemFontOfSize:15];
        rightNameLb.numberOfLines = 0;
        [teamInfoView addSubview:rightNameLb];
        rightNameLb.text = @"Manchester United";
        self.rightNameLb = rightNameLb;
        
        UILabel *rightScoreLb = [UILabel new];
        rightScoreLb.textAlignment = NSTextAlignmentCenter;
//        rightScoreLb.backgroundColor = [UIColor grayColor];
        rightScoreLb.font = [UIFont boldSystemFontOfSize:18];
        rightScoreLb.textColor = UIColorFromRGB(182, 182, 182);
        rightScoreLb.numberOfLines = 0;
        rightScoreLb.text = @"111";
        [teamInfoView addSubview:rightScoreLb];
        self.rightScoreLb = rightScoreLb;

        UIView *teamMatchInfoView = [UIView new];
        teamMatchInfoView.backgroundColor = [UIColor whiteColor];
        teamMatchInfoView.clipsToBounds = YES;
        teamMatchInfoView.layer.cornerRadius = 5;
        [hView insertSubview:teamMatchInfoView belowSubview:teamInfoView];
        
        UILabel *leftRateTitle = [UILabel new];
        leftRateTitle.backgroundColor = UIColorFromRGB(253, 109, 55);
        leftRateTitle.textColor = [UIColor whiteColor];
        leftRateTitle.textAlignment = NSTextAlignmentCenter;
        leftRateTitle.clipsToBounds = YES;
        leftRateTitle.layer.cornerRadius = kWinRateLabelWidth/2;
        leftRateTitle.text = @"25%";
        leftRateTitle.layer.borderColor = UIColorFromRGB(233, 233, 233).CGColor;
        leftRateTitle.layer.borderWidth = 3;
        [teamMatchInfoView addSubview:leftRateTitle];
        self.leftRateTitle = leftRateTitle;
        
        UILabel *leftWinRateTitle = [UILabel new];
        leftWinRateTitle.textColor = [UIColor blackColor];
        leftWinRateTitle.textAlignment = NSTextAlignmentCenter;
        leftWinRateTitle.numberOfLines = 0;
        leftWinRateTitle.font = [UIFont systemFontOfSize:12];
        leftWinRateTitle.text = @"Historical win rate";
        [teamMatchInfoView addSubview:leftWinRateTitle];
        
        SDHSportsRatioResultView *leftRatioResultView = [[SDHSportsRatioResultView alloc] init];
        [teamMatchInfoView addSubview:leftRatioResultView];
        self.leftRatioResultView = leftRatioResultView;
        
        UILabel *leftLastGamesLb = [UILabel new];
        leftLastGamesLb.text = @"Last 6 games";
        leftLastGamesLb.textColor = [UIColor grayColor];
//        leftLastGamesLb.backgroundColor = [UIColor greenColor];
        leftLastGamesLb.textAlignment = NSTextAlignmentLeft;
        leftLastGamesLb.font = [UIFont systemFontOfSize:10];
        [teamMatchInfoView addSubview:leftLastGamesLb];
        
        SDHSportsWinRateView *rateView = [SDHSportsWinRateView new];
        rateView.backgroundColor = [UIColor yellowColor];
        [teamMatchInfoView addSubview:rateView];
        [rateView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(teamMatchInfoView);
            make.top.equalTo(leftRateTitle.mas_top).offset(20);
            make.height.equalTo(@5);
        }];
        self.sratioView = rateView;

        SDHSportsWinRateView *rateView1 = [SDHSportsWinRateView new];
        rateView1.backgroundColor = [UIColor yellowColor];
        [teamMatchInfoView addSubview:rateView1];
        [rateView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(teamMatchInfoView);
            make.top.equalTo(rateView).offset(20);
            make.height.equalTo(@5);
        }];
        self.recentSratioView = rateView1;

        SDHSportsWinRateView *rateView2 = [SDHSportsWinRateView new];
        rateView2.backgroundColor = [UIColor yellowColor];
        [teamMatchInfoView addSubview:rateView2];
        [rateView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(teamMatchInfoView);
            make.top.equalTo(rateView1).offset(20);
            make.height.equalTo(@5);
        }];
        self.seasonSratioView = rateView;
        
        [leftRateTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(teamMatchInfoView).offset(10);
            make.top.equalTo(teamMatchInfoView).offset(60);
            make.width.equalTo(@(kWinRateLabelWidth));
            make.height.equalTo(@(kWinRateLabelWidth));
        }];
        
        [leftWinRateTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(leftRateTitle.mas_bottom).offset(5);
            make.left.equalTo(teamMatchInfoView).offset(5);
            make.right.equalTo(leftRateTitle.mas_right).offset(10);
        }];
        
        [leftRatioResultView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(teamMatchInfoView).offset(5);
            make.top.equalTo(leftWinRateTitle.mas_bottom).offset(10);
        }];
        
        [leftLastGamesLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(teamMatchInfoView).offset(5);
            make.top.equalTo(leftRatioResultView.mas_bottom).offset(5);
        }];
        
        UILabel *rightRateTitle = [UILabel new];
        rightRateTitle.backgroundColor = rightTeamTitleColor;
        rightRateTitle.textColor = [UIColor whiteColor];
        rightRateTitle.textAlignment = NSTextAlignmentCenter;
        rightRateTitle.clipsToBounds = YES;
        rightRateTitle.layer.cornerRadius = kWinRateLabelWidth/2;
        rightRateTitle.text = @"75%";
        rightRateTitle.layer.borderColor = UIColorFromRGB(233, 233, 233).CGColor;
        rightRateTitle.layer.borderWidth = 3;
        [teamMatchInfoView addSubview:rightRateTitle];
        self.rightRateTitle = rightRateTitle;
        
        UILabel *rightWinRateTitle = [UILabel new];
        rightWinRateTitle.textColor = [UIColor blackColor];
        rightWinRateTitle.textAlignment = NSTextAlignmentCenter;
        rightWinRateTitle.numberOfLines = 0;
        rightWinRateTitle.font = [UIFont systemFontOfSize:12];
        rightWinRateTitle.text = @"Historical win rate";
        [teamMatchInfoView addSubview:rightWinRateTitle];
        
        SDHSportsRatioResultView *rightRatioResultView = [[SDHSportsRatioResultView alloc] init];
        [teamMatchInfoView addSubview:rightRatioResultView];
        self.rightRatioResultView = rightRatioResultView;
                
        UILabel *rightLastGamesLb = [UILabel new];
        rightLastGamesLb.text = @"Last 6 games";
        rightLastGamesLb.textColor = [UIColor grayColor];
//        rightLastGamesLb.backgroundColor = [UIColor greenColor];
        rightLastGamesLb.textAlignment = NSTextAlignmentRight;
        rightLastGamesLb.font = [UIFont systemFontOfSize:10];
        [teamMatchInfoView addSubview:rightLastGamesLb];
        
        __weak typeof(self) weakSelf = self;
        [rightRateTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(teamMatchInfoView).offset(-10);
            make.top.equalTo(teamMatchInfoView).offset(60);
            make.width.equalTo(@(kWinRateLabelWidth));
            make.height.equalTo(@(kWinRateLabelWidth));
        }];
        
        [rightWinRateTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(rightRateTitle.mas_bottom).offset(5);
            make.right.equalTo(teamMatchInfoView).offset(-5);
            make.left.equalTo(rightRateTitle.mas_left).offset(-10);
        }];
        
        [rightRatioResultView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(teamMatchInfoView).offset(-5);
            make.top.equalTo(rightWinRateTitle.mas_bottom).offset(10);
        }];
        
        [rightLastGamesLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(teamMatchInfoView).offset(-5);
            make.top.equalTo(rightRatioResultView.mas_bottom).offset(5);
        }];
        
        [teamInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(hView).offset(5);
            make.left.equalTo(hView).offset(10);
            make.right.equalTo(hView).offset(-10);
//            make.bottom.equalTo(leftNameLb.mas_bottom).offset(10);
            make.height.equalTo(@(kTeamInfoHeight));
        }];
        
        [teamMatchInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(teamInfoView.mas_bottom).offset(-50);
            make.left.equalTo(hView).offset(5);
            make.right.equalTo(hView).offset(-5);
            make.bottom.equalTo(hView).offset(-5);
        }];
        
        UIView *superview = teamInfoView;
        [leftAvatarImg mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(superview).offset(25);
            make.top.equalTo(superview).offset(20);
            make.width.equalTo(@30);
            make.height.equalTo(@30);
        }];
        
        [leftNameLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(leftAvatarImg);
            make.top.equalTo(leftAvatarImg.mas_bottom).offset(8);
            make.left.equalTo(leftAvatarImg).offset(-15);
            make.right.equalTo(leftAvatarImg).offset(15);
        }];
        
        [leftScoreLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(leftNameLb.mas_right).offset(10);
            make.centerY.equalTo(leftAvatarImg);
            make.width.equalTo(@30);
            make.height.equalTo(@30);
        }];
        
        [vsImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(teamInfoView);
            make.top.equalTo(leftScoreLb);
            make.width.equalTo(@30);
            make.height.equalTo(@30);
        }];
        
        [timeLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(timeLb.superview);
            make.top.equalTo(leftNameLb.mas_top);
        }];
        
        // right
        [rightAvatarImg mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(superview).offset(-25);
            make.top.equalTo(superview).offset(20);
            make.width.equalTo(@30);
            make.height.equalTo(@30);
        }];
        
        [rightNameLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(rightAvatarImg);
            make.top.equalTo(rightAvatarImg.mas_bottom).offset(8);
            make.left.equalTo(rightAvatarImg).offset(-15);
            make.right.equalTo(rightAvatarImg).offset(15);
        }];
        
        [rightScoreLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(rightNameLb.mas_left).offset(-10);
            make.centerY.equalTo(rightAvatarImg);
            make.width.equalTo(@30);
            make.height.equalTo(@30);
        }];
    }
    
    return self;
}

- (void)updateHeaderTeam:(SDHSportsInteListModel *)model {
    [self.leftAvatarImg setImageWithURL:[NSURL URLWithString:model.guest_logo] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    [self.rightAvatarImg setImageWithURL:[NSURL URLWithString:model.host_logo] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    self.leftNameLb.text = model.guest_en_name;
    self.rightNameLb.text = model.host_en_name;
    self.timeLb.text = [NSString stringWithFormat:@" %@ ", model.time];
}

- (void)updateHeaderRatio:(SDHSportsInteDetailModel *)model {
    self.leftScoreLb.text = model.guest_score;
    self.rightScoreLb.text = model.host_score;
    self.leftRateTitle.text = [NSString stringWithFormat:@"%@%%", model.guest_sratio];
    self.rightRateTitle.text = [NSString stringWithFormat:@"%@%%", model.host_sratio];
    
    [self.sratioView updateLeft:[model.guest_sratio floatValue] right:[model.host_sratio floatValue]];
    [self.recentSratioView updateLeft:[model.guest_recent_sratio floatValue] right:[model.host_recent_sratio floatValue]];
    [self.recentSratioView updateLeft:[model.guest_season_sratio floatValue] right:[model.host_season_sratio floatValue]];
    
    [self.leftRatioResultView updateContent:model.guest_six_result];
    [self.rightRatioResultView updateContent:model.host_six_result];
}

- (void)avatarClick:(UIPanGestureRecognizer *)gesture {
    if (gesture.view == self.leftAvatarImg) {
        if (self.leftAvatarClick) {
            self.leftAvatarClick();
        }
    } else {
        if (self.rightAvatarClick) {
            self.rightAvatarClick();
        }
    }
}

@end
